package com.gqz.controller.system;

import com.gqz.domain.BlogArticle;
import com.gqz.query.BaseQuery;
import com.gqz.service.IBlogArticleService;
import com.gqz.utils.JsonResult;
import com.gqz.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/sys/blog/article")
public class SysBlogArticleController {

    @Autowired
    private IBlogArticleService blogArticleService;

    /**
     * 查询 允许空值上传
     *
     * @param query
     * @return
     */
    @GetMapping({"/{pageIndex}/{pageSize}/{keyword}", "/{pageIndex}/{pageSize}", ""})
    public JsonResult list(BaseQuery query) throws Exception {
        PageList<BlogArticle> list = blogArticleService.getAllArticle(query);
        return JsonResult.ok(list);
    }

    @DeleteMapping
    public JsonResult remove(@RequestBody Long[] ids) {
        boolean result = blogArticleService.batchDeleteArticle(ids);
        return result ? JsonResult.ok() : JsonResult.fail();
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable Long id) {
        boolean result = blogArticleService.deleteArticle(id);
        return JsonResult.toAjax(result);
    }

    @PutMapping
    public JsonResult edit(@RequestBody BlogArticle article) {
        boolean result = blogArticleService.updateArticle(article);
        return JsonResult.toAjax(result);
    }

    @PostMapping
    public JsonResult add(@RequestBody BlogArticle article, HttpServletRequest request) {
        boolean result = blogArticleService.insertArticle(article, request);
        return JsonResult.toAjax(result);
    }

}
